import { combineReducers, configureStore } from "@reduxjs/toolkit";
import CounterReducer from "./modules/counterSlice"
import UserReducer from "./modules/userSlice"

import storage from 'redux-persist/lib/storage'
import { persistStore, persistReducer } from 'redux-persist'

const persistConfig = {
    key: 'root',
    storage, // 默认是localstorage
    // whitelist: ['counter'] // 标识仅持久化哪些reducer
    // blacklist: ['user'] // 用来排除哪些reducer
}

const usePersistConfig = {
    key: 'user',
    storage,
    whitelist: ['name'] // 控制哪些字段需要缓存
}

const persistedReducer = persistReducer(persistConfig, combineReducers({
    counter: CounterReducer,
    user: persistReducer(usePersistConfig, UserReducer)
}))

const Store = configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) => getDefaultMiddleware({
        serializableCheck: false,
    }),
})

export const persistor = persistStore(Store)
export default Store


